/*
 * 代码生成器自动生成的
 * Since 2008 - 2019
 *
 */
package cn.com.bod.fas.cim.service.impl;

import cn.com.bod.fas.cim.dao.mapper.DmBaseInfoMapper;
import cn.com.bod.fas.cim.dao.mapper.DmCorporationMapper;
import cn.com.bod.fas.cim.dao.mapper.DmIndvBizesMapper;
import cn.com.bod.fas.cim.model.po.DmCorporationPo;
import cn.com.bod.fas.cim.model.vo.request.DmCorporationIndvRequVo;
import cn.com.bod.fas.cim.model.vo.response.DmDealerInfoRespVo;
import cn.com.bod.fas.cim.service.DmBaseInfoService;
import cn.com.bod.fas.common.utils.ResultUtil;
import cn.com.bod.fas.common.utils.UserInfoUtil;
import cn.com.bod.fas.common.utils.VoTransformUtil;
import cn.com.bod.fas.cim.constant.DealerConstants;
import cn.com.bod.fas.cim.model.po.DmBaseInfoPo;
import cn.com.bod.fas.cim.model.po.DmIndvBizesPo;
import cn.com.bod.fas.cim.model.vo.DmBaseInfoVo;
import cn.com.bod.fas.cim.model.vo.DmCorporationVo;
import cn.com.bod.fas.cim.model.vo.DmIndvBizesVo;
import cn.com.bod.fas.cim.model.vo.response.DmBaseCorporationRespVo;
import cn.com.bod.fas.cim.model.vo.response.DmBaseIndvRespVo;
import cn.com.yusys.yusp.commons.mapper.QueryModel;
import cn.com.yusys.yusp.commons.web.rest.dto.ResultDto;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * @author zbb
 * @version 1.0.0
 * @description 经销商基本信息实现类
 * @date 2019年9月16日 14:29:07
 * @Copyright (c) scfhdt
 */
@Service
public class DmBaseInfoServiceImpl implements DmBaseInfoService {

    @Autowired
    private DmBaseInfoMapper dmBaseInfoMapper;
    @Autowired
    private DmIndvBizesMapper dmIndvBizesMapper;
    @Autowired
    private DmCorporationMapper dmCorporationMapper;
    @Autowired
    private ResultUtil resultUtil;
    @Autowired
    private UserInfoUtil userInfoUtil;


    /**
     * @description 查询经销商列表（不分页）
     * @author zbb
     * @date 2019年9月26日 19:35
     * @param model
     * @return java.util.List
     */
    @Override
    public List<DmDealerInfoRespVo> selectAll(QueryModel model) {

        return VoTransformUtil.listCopy(dmBaseInfoMapper.selectByModel(model), DmDealerInfoRespVo::new);
    }

    /**
     * @description 查询经销商列表（分页）
     * @author zbb
     * @date 2019年9月26日 19:35
     * @param model
     * @return java.util.List
     */
    @Override
    public List<DmBaseInfoVo> selectByModel(QueryModel model) {
        String queryWord = (String) model.getCondition().get("queryWord");
        if (!StringUtils.isEmpty(queryWord)) {
            if (queryWord.matches("^[0-9]+$")) {
                model.getCondition().put("socCrdtCode", queryWord);
            } else {
                model.getCondition().put("dealerName", queryWord);
            }

        }

        PageHelper.startPage(model.getPage(), model.getSize());
        List<DmBaseInfoVo> list = dmBaseInfoMapper.selectByModel(model);
        PageHelper.clearPage();
        return list;
    }

    /**
     * @description 删除经销商信息
     * @author zbb
     * @date 2019年9月26日 23:57
     * @param dealerNo
     * @return void
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteByPrimaryKey(String dealerNo) {

        DmBaseInfoVo dmBaseInfo = dmBaseInfoMapper.selectByPrimaryKey(dealerNo);
        String dealerTypeId = dmBaseInfo.getDealerTypeId();
        dmBaseInfoMapper.deleteByPrimaryKey(dealerNo);
        if (DealerConstants.dealerType_indv.equals(dealerTypeId)) {
            dmIndvBizesMapper.deleteByPrimaryKey(dealerNo);
        } else {
            dmCorporationMapper.deleteByPrimaryKey(dealerNo);
        }

    }

    /**
     * @description 经销商详情
     * @author zbb
     * @date 2019年9月29日 18:15
     * @param dealerNo
     * @return cn.com.scfhdt.dealer.model.vo.DmBaseInfoVo
     */

    @Override
    public DmBaseInfoVo selectDealerInfo(String dealerNo){
        return  dmBaseInfoMapper.selectByPrimaryKey(dealerNo);
    }

    /**
     * @description 查询经销商详情
     * @author zbb
     * @date 2019年9月26日 23:58
     * @param dealerNo, dealerType
     * @return cn.com.yusys.yusp.commons.web.rest.dto.ResultDto
     */
    @Override
    public ResultDto selectDealerInfo(String dealerNo, String dealerTypeId) {

        if (dealerTypeId.equals(DealerConstants.dealerType_indv)) {
            DmBaseIndvRespVo baseIndvRespVo = dmBaseInfoMapper.selectDealerIndvInfo(dealerNo);

            return resultUtil.success(baseIndvRespVo);
        } else {
            DmBaseCorporationRespVo dmBaseCorporationRespVo = dmBaseInfoMapper.selectDealerCorporationInfo(dealerNo);
            return resultUtil.success(dmBaseCorporationRespVo);
        }


    }

    /**
     * @description 经销商新增和修改
     * @author zbb
     * @date 2019年9月26日 23:59
     * @param dmCorporationIndvRequVo
     * @return void
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addAndUpdate(DmCorporationIndvRequVo dmCorporationIndvRequVo) {

        DmBaseInfoVo dmBaseInfoVo = dmCorporationIndvRequVo.getDmBaseInfoVo();
        String dealerNo = new SimpleDateFormat("yyyyMMddhhmmssSSS").format(new Date());
        int rannum = (int) ((Math.random() * 9 + 1) * 100000);
        int rannum1 = (int) (new Random().nextDouble() * (99999 - 10000 + 1)) + 10000;
        if (StringUtils.isEmpty(dmBaseInfoVo.getDealerNo())) {

            addDealer(dmCorporationIndvRequVo, dmBaseInfoVo, dealerNo, rannum, rannum1);

        } else {
            updateDearler(dmCorporationIndvRequVo, dmBaseInfoVo);

        }

    }

    private void updateDearler(DmCorporationIndvRequVo dmCorporationIndvRequVo, DmBaseInfoVo dmBaseInfoVo) {
        DmBaseInfoPo dmBaseInfo = new DmBaseInfoPo();
        BeanUtils.copyProperties(dmBaseInfoVo, dmBaseInfo);
        dmBaseInfoMapper.updateByPrimaryKeySelective(dmBaseInfo);

        if (DealerConstants.dealerType_indv.equals(dmBaseInfoVo.getDealerTypeId())) {
            DmIndvBizesVo dmIndvBizesVo = dmCorporationIndvRequVo.getDmIndvBizesVo();
            DmIndvBizesPo dmIndvBizes = new DmIndvBizesPo();
            BeanUtils.copyProperties(dmIndvBizesVo, dmIndvBizes);
            userInfoUtil.setRecentReviser(dmIndvBizes);
            dmIndvBizesMapper.updateByPrimaryKeySelective(dmIndvBizes);

        } else {
            DmCorporationVo dmBaseCorporationDto = dmCorporationIndvRequVo.getDmCorporationVo();
            DmCorporationPo dmCorporation = new DmCorporationPo();
            BeanUtils.copyProperties(dmBaseCorporationDto, dmCorporation);
            userInfoUtil.setRecentReviser(dmCorporation);
            dmCorporationMapper.updateByPrimaryKeySelective(dmCorporation);
        }
    }

    private void addDealer(DmCorporationIndvRequVo dmCorporationIndvRequVo, DmBaseInfoVo dmBaseInfoVo, String dealerNo, int rannum, int rannum1) {
        DmBaseInfoPo dmBaseInfo = new DmBaseInfoPo();
        BeanUtils.copyProperties(dmBaseInfoVo, dmBaseInfo);
        dmBaseInfo.setDealerId(rannum);
        dmBaseInfo.setDealerNo(dealerNo);
        userInfoUtil.setCreator(dmBaseInfo);
        dmBaseInfo.setCustMgrNo(dmBaseInfo.getCreatorId().toString());
        dmBaseInfoMapper.insertSelective(dmBaseInfo);

        if (DealerConstants.dealerType_indv.equals(dmBaseInfoVo.getDealerTypeId())) {
            DmIndvBizesVo dmIndvBizesVo = dmCorporationIndvRequVo.getDmIndvBizesVo();
            DmIndvBizesPo dmIndvBizes = new DmIndvBizesPo();
            BeanUtils.copyProperties(dmIndvBizesVo, dmIndvBizes);
            dmIndvBizes.setIndvBizesId(rannum1);
            dmIndvBizes.setDealerNo(dealerNo);
            userInfoUtil.setCreator(dmIndvBizes);
            dmIndvBizesMapper.insertSelective(dmIndvBizes);

        } else {
            DmCorporationVo dmCorporationVo = dmCorporationIndvRequVo.getDmCorporationVo();
            DmCorporationPo dmCorporation = new DmCorporationPo();
            BeanUtils.copyProperties(dmCorporationVo, dmCorporation);
            dmCorporation.setCorpId(rannum1);
            dmCorporation.setDealerNo(dealerNo);
            userInfoUtil.setCreator(dmCorporation);
            userInfoUtil.setRecentReviser(dmCorporation);
            dmCorporationMapper.insertSelective(dmCorporation);
        }
    }

}

